package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabFolder2Adapter;
import org.eclipse.swt.custom.CTabFolderEvent;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class SwtSashForm {

    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setText("SashForm Demo");
        shell.setLayout(new FillLayout());
       
        final SashForm form = new SashForm(shell,SWT.HORIZONTAL|SWT.FLAT);
        form.setLayout(new FillLayout());
//        Composite left = new Composite(form,SWT.NONE);
//        left.setLayout(new FillLayout());
        new Text(form, SWT.MULTI|SWT.V_SCROLL).setText("分割窗口");
       
        final CTabFolder ctf = new CTabFolder(form, SWT.BORDER);
        ctf.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
        ctf.setSimple(false);
        ctf.setUnselectedImageVisible(true);
        ctf.setUnselectedCloseVisible(true);
        ctf.setMinimizeVisible(true);
        ctf.setMaximizeVisible(true);
        ctf.setSelectionBackground(display.getSystemColor(SWT.COLOR_YELLOW));
        for (int i=1;i<3;i++){
            CTabItem cti = new CTabItem(ctf, SWT.CLOSE);
            cti.setText("选项卡 "+i);
        }
       
//        left.layout();
        ctf.layout();
        ctf.setFocus();
       
        ctf.addCTabFolder2Listener(new CTabFolder2Adapter(){
            public void minimize(CTabFolderEvent event) {
                // 设置选项卡的状态为最小化，选项卡的状态决定显示在右上角的窗口按钮
                ctf.setMinimized(true);
                // 改变选项卡的布局，呈现最小化状态
                ctf.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
                shell.layout(true);// 刷新布局，否则重新设置的布局将不起作用
            }

            // 当单击最大化按钮时触发的事件
            public void maximize(CTabFolderEvent event) {
                ctf.setMaximized(true);
                ctf.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,true));
                form.setMaximizedControl(ctf);
                shell.layout(true);
            }

            // 当单击还原按钮时触发的事件
            public void restore(CTabFolderEvent event) {
                ctf.setMinimized(false);
                ctf.setMaximized(false);
                ctf.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,false));
                form.setMaximizedControl(null);
                shell.layout(true);
            }
           
        });
        form.setWeights(new int[] {30,70});
        shell.setSize(500, 350);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
       
    }

}